var checkForm = {
	'email':{
		preg:/^[a-z0-9\.]+@[a-z0-9]+\.[a-z]+$/i,
		focus:'请填写你的邮箱!',
		empty:'邮箱不能为空',
		error:'邮箱格式错误'
		
	},
	'uname':{
		preg:/^[a-z]\w{5,15}$/i,
		focus:'请填写你的用户名!',
		empty:'用户名不能为空',
		error:'用户名格式错误'
	},
	'password':{
		preg:/^\S{6,32}$/,
		focus:'请填写你的密码!',
		empty:'密码不能为空',
		error:'密码格式错误'	
		
	},
	'password_d':{
		focus:'确认密码!',
		empty:'密码不能为空',
		error:'密码不一致'
	},
	'code':{
		preg:/^[a-z0-9]{4}$/i,
		focus:'请输入验证码!',
		empty:'验证码不能为空',
		error:'验证码格式错误'
	}
};

$(function(){
	checkform();
	$('[name="email"]').focus();
	$('#regForm').submit(function() {
		for(var i=0; i<eles.length;i++)
		{
			if(eles[i].status===false)
				return false;
		}
	});
})
var eles = [];
/**
 * [checkform 验证]
 * @return {[type]} [description]
 */
function checkform()
{
	var must = $('#regForm .must');
	must.each(function(){
		eles.push(this);
		this.status=false;
	})


	for(var i = 0;i<eles.length;i++)
	{
		eles[i].onfocus=function()
		{
			var name = this.name;
			var msg = checkForm[name]['focus'];
			show_focus.call(this,msg);
			this.onblur = function()
			{
				var val= this.value;
				// 必填验证
				if(val =='')
				{
					var msg = checkForm[name]['empty'];
					show_error.call(this,msg);
					return;
				}
				// 密码不一致
				if(name=='password_d')
				{
					if($('#password').val()!=val)
					{
						var msg = checkForm[name]['error'];
						show_error.call(this,msg);
						return;
					}
				}
				else
				{ 
					// 正则验证
					var preg = checkForm[name]['preg'];
					if(!preg.test(val))
					{
						var msg = checkForm[name]['error'];
						show_error.call(this,msg);
						return;
					}
				}

				//需要ajax校验的
				if($(this).attr('ajax') == 1)
				{
					var self = this;
					$.ajax({
						url:CONTROL+'&m=ajaxcheck',
						type:'POST',
						data:name+'='+val,
						dataType:'json',
						success:function(result)
						{
							if(result.status === true)
							{
								show_success.call(self,'');
							}else
							{
								show_error.call(self,result.msg);
							}
						}
					})
				}
				else
				{
					show_success.call(this,'');
				}
			}
		}
	}

}

/**
 * [show_success 显示通过]
 * @param  {[type]} msg [description]
 * @return {[type]}     [description]
 */
function show_success(msg)
{
	var parent = $(this).parents('dl');
	var oPrompt = parent.find('.prompt');
	parent.attr('class','success');
	oPrompt.html(msg);
	this.status = true;
}

/**
 * [show_focus 显示焦点]
 * @param  {[type]} msg [description]
 * @return {[type]}     [description]
 */
function show_focus(msg)
{
	var parent = $(this).parents('dl');
	var oPrompt = parent.find('.prompt');
	parent.attr('class','focus');
	oPrompt.html(msg);
}

/**
 * [show_error 显示错误]
 * @param  {[type]} msg [description]
 * @return {[type]}     [description]
 */
function show_error(msg)
{
	var parent = $(this).parents('dl');
	var oPrompt = parent.find('.prompt');
	parent.attr('class','error');
	oPrompt.html(msg);
	this.status = false;
}